package com.ekang.admin.web;

import com.ekang.admin.common.BaseResult;
import com.ekang.admin.model.Goods;
import com.ekang.admin.model.Order;
import com.ekang.admin.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * Created by zcz on 2017/10/25.
 */
@Controller
public class GoodsController {


    @Autowired
    GoodsService goodsService;

    @RequestMapping(value = "/goodsList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult<List<Goods>>> getGoodsList() throws Exception {
        List<Goods> goodsList = goodsService.getGoodsList(null);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(goodsList);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/goods/{goodsID}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> getGoods(@PathVariable Integer goodsID) throws Exception {
        Goods goods = goodsService.getGoodsById(goodsID);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(goods);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<BaseResult> addOrder(@RequestBody Order order) throws Exception {
        Order data = goodsService.addOrder(order);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }

    @RequestMapping(value = "/order/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> getOrder(@PathVariable Integer orderId) throws Exception {
        Order order = goodsService.getOrderById(orderId);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(order);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }

    @RequestMapping(value = "/orderInfo/{orderNO}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> addOrder(@PathVariable String orderNO) throws Exception {
        Order order = goodsService.getOrderByOrderNO(orderNO);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(order);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/order/pay", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<BaseResult> payOrder(@RequestBody Order order) throws Exception {
        Order data = goodsService.updateOrder(order);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }

    @RequestMapping(value = "/orderList/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> getUserOrders(@PathVariable String userId) throws Exception {
        Object data = goodsService.getOrderListByUserId(userId);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/goodsState/{type}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> updateGoodsState(@PathVariable String type, @PathVariable String state) throws Exception {
        Object data = goodsService.updateGoodsState(type, state);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/orders", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> getOrders() throws Exception {
        Object data = goodsService.getOrderList(null);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }

    @RequestMapping(value = "/orderState/{id}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> updateOrderState(@PathVariable Integer id, @PathVariable Integer state) throws Exception {
        Order order = new Order();
        order.setOrderId(id);
        order.setStatus(state);
        Object data = goodsService.updateOrderById(order);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/order.html", method = RequestMethod.GET)
    public String orderHtml(HashMap<String, Object> map) {
        return "/order";
    }


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> testGoods() throws Exception {
        Object data = goodsService.getGoodsList(null);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }
}
